package pms.sg.service;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.jdbc.support.rowset.SqlRowSet;

import pms.common.value.ProjectValue;
import pms.common.value.sgListValue;
import pms.common.value.sgSearchValue;
import pms.common.dao.ListSGDao;
import pms.common.dao.ProjectDao;

public class ListSGServiceImpl implements ListSGService {
	private ListSGDao listSGDao;
	private ProjectDao projectDao;

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	private sgListValue zhlist;
	
	public ListSGDao getListSGDao() {
		return listSGDao;
	}

	public void setListSGDao(ListSGDao listSGDao) {
		this.listSGDao = listSGDao;
	}

	public boolean deleteData(String[] banGos) {
		return listSGDao.deleteData(banGos);
	}
	
	public InputStream expExcelData(String[] banGos) throws Exception {
		return listSGDao.expData(banGos);
	}
	
	public List<ProjectValue> getProjectList() {
		return projectDao.getProjectList();
	}
   @SuppressWarnings("unchecked")
public List<HashMap> getFunctionArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		return listSGDao.getFunctionArray();
    }
   @SuppressWarnings("unchecked")
public List<HashMap> getTestlvlArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
       SqlRowSet l_rs = listSGDao.getMasterData("test_level");
       while(l_rs.next()) {
       	HashMap l_hashMap = new HashMap();
       	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
       	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
       	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
       	l_array.add(l_hashMap);
       }
       System.out.println("TEST_LVL:" + l_array);
       return l_array;
   }
   @SuppressWarnings("unchecked")
   public List<HashMap> getTestPartArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
      SqlRowSet l_rs = listSGDao.getMasterData("test_part");
      while(l_rs.next()) {
      	HashMap l_hashMap = new HashMap();
      	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
      	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
      	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
      	l_array.add(l_hashMap);
      }
      System.out.println("TEST_PART:" + l_array);
      return l_array;
  }
	@SuppressWarnings("unchecked")
	public List<HashMap> getUserArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		List<HashMap> l_array = new ArrayList();
		l_array = listSGDao.getUserData();
		return l_array;
	}
	@SuppressWarnings("unchecked")
	public List<HashMap> getStatusArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
      SqlRowSet l_rs = listSGDao.getMasterData("bug_status");
      while(l_rs.next()) {
      	HashMap l_hashMap = new HashMap();
      	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
      	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
      	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
      	l_array.add(l_hashMap);
      }
      System.out.println("TEST_PART:" + l_array);
      return l_array;
  }
	@SuppressWarnings("unchecked")
	public List<HashMap> getReasonArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
      SqlRowSet l_rs = listSGDao.getMasterData("bug_reason");
      while(l_rs.next()) {
      	HashMap l_hashMap = new HashMap();
      	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
      	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
      	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
      	l_array.add(l_hashMap);
      }
      System.out.println("TEST_PART:" + l_array);
      return l_array;
  }
	@SuppressWarnings("unchecked")
	public ArrayList getSGLists(sgSearchValue sgsearch) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		ArrayList zhliststmp = new ArrayList();
		SqlRowSet l_rs = listSGDao.getListData(sgsearch);
		HashMap proList = listSGDao.getProject();
		HashMap themeList = listSGDao.getTheme();
    	while(l_rs.next()) {
    		zhlist = new sgListValue();
    		zhlist.setNo(l_rs.getString("ZH_NO").trim());
    		if(proList.containsKey(String.valueOf(l_rs.getString("PROJECTID").trim()))){
    			zhlist.setProject(String.valueOf(proList.get(l_rs.getString("PROJECTID").trim())));
    		} else {
    			zhlist.setProject(l_rs.getString("PROJECTID").trim());
    		}
    		if(themeList.containsKey(String.valueOf(l_rs.getString("TEMA").trim()))){
    			zhlist.setTema(String.valueOf(themeList.get(l_rs.getString("TEMA").trim())));
    		} else {
    			zhlist.setTema(l_rs.getString("TEMA").trim());
    		}
    		zhlist.setTest_lvl(l_rs.getString("TEST_LVL").trim());
    		zhlist.setTest_part(l_rs.getString("TEST_PART").trim());
    		zhlist.setTester(l_rs.getString("TESTER").trim());
    		zhlist.setTest_date(l_rs.getString("TEST_DATE").trim().substring(0,4)+"/"+l_rs.getString("TEST_DATE").trim().substring(4,6)+"/"+l_rs.getString("TEST_DATE").trim().substring(6));
    		zhlist.setStatus(l_rs.getString("ZH_STATUS").trim());
    		zhlist.setMemo(l_rs.getString("MEMO").trim());
    		zhlist.setLimit_date(l_rs.getString("LIMIT_DATE").trim().substring(0,4)+"/"+l_rs.getString("LIMIT_DATE").trim().substring(4,6)+"/"+l_rs.getString("LIMIT_DATE").trim().substring(6));
    		zhlist.setModify(l_rs.getString("ZH_MODIFY").trim());
    		zhlist.setReason(l_rs.getString("REASON").trim());
    		zhliststmp.add(zhlist);
		}
    	return zhliststmp;
	}
	public String getDownloadFileName() throws UnsupportedEncodingException {
		String downFileName = "sg_export_list.xls"; 
		downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		return downFileName; 
	}
}

